home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / scribus-ng / scripts / InfoBox.py < prev   
Encoding:
Python Source  |  2008-07-25  |  7.2 KB  |  161 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8  -*-
  3.  
  4. # ****************************************************************************
  5. #  This program is free software; you can redistribute it and/or modify 
  6. #  it under the terms of the GNU General Public License as published by
  7. #  the Free Software Foundation; either version 2 of the License, or
  8. #  (at your option) any later version.
  9. #
  10. #  This program is distributed in the hope that it will be useful,
  11. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #  GNU General Public License for more details.
  14. #
  15. #  You should have received a copy of the GNU General Public License
  16. #  along with this program; if not, write to the Free Software
  17. #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. # ****************************************************************************
  19.  
  20.  
  21. """
  22. (C) 2005 by Thomas R. Koll, <tomk32@gmx.de>, http://verlag.tomk32.de
  23. (c) 2008 modifications, additional features by Gregory Pittman
  24.  
  25. A simple script for exact placement of a frame (infobox)
  26. over the current textbox, asking the user for the width
  27. of the infobox and in which column to place it.
  28. Some enhancements:
  29. * You can now create a text frame or an image frame, and also load
  30. an image.
  31. * More than one infobox can be added to a text frame
  32. * Height and Y-Pos of top of infobox can be specified
  33. * Works with any page unit - pts, mm, in, and picas
  34. * Infobox has Text Flows Around Frame activated, also
  35.   Scale Image to Frame for images
  36.  
  37. USAGE
  38. Select a textframe, start the script and have phun
  39. Default name for the infobox is 'infobox' + name_of_selected_frame,
  40. but this can be changed.
  41.  
  42.  
  43. """
  44.  
  45. try:
  46.     import scribus
  47. except ImportError:
  48.     print "Unable to import the 'scribus' module. This script will only run within"
  49.     print "the Python interpreter embedded in Scribus. Try Script->Execute Script."
  50.     sys.exit(1)
  51. try:
  52.     from PIL import Image
  53. except ImportError:
  54.     print "Unable to import the Python Imaging Library module."
  55.     sys.exit(1)
  56.  
  57. def main(argv):
  58.     unit = scribus.getUnit()
  59.     units = [' pts','mm',' inches',' picas','cm',' ciceros']
  60.     unitlabel = units[unit]
  61.     if scribus.selectionCount() == 0:
  62.         scribus.messageBox('Scribus - Script Error',
  63.             "There is no object selected.\nPlease select a text frame and try again.",
  64.             scribus.ICON_WARNING, scribus.BUTTON_OK)
  65.         sys.exit(2)
  66.     if scribus.selectionCount() > 1:
  67.         scribus.messageBox('Scribus - Script Error',
  68.             "You have more than one object selected.\nPlease select one text frame and try again.",
  69.             scribus.ICON_WARNING, scribus.BUTTON_OK)
  70.         sys.exit(2)
  71.     textbox = scribus.getSelectedObject()
  72.     pageitems = scribus.getPageItems()
  73.     boxcount = 1
  74.     for item in pageitems:
  75.         if (item[0] == textbox):
  76.             if (item[1] != 4):
  77.                 scribus.messageBox('Scribus - Script Error', 
  78.                           "This is not a textframe. Try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
  79.                 sys.exit(2)
  80.  
  81. # While we're finding out what kind of frame is selected, we'll also make sure we
  82. # will come up with a unique name for our infobox frame - it's possible we may want
  83. # more than one for a multicolumn frame.
  84.         if (item[0] == ("infobox" + str(boxcount) + textbox)):
  85.                 boxcount += 1
  86.     left, top = scribus.getPosition(textbox)
  87.     o_width, o_height = scribus.getSize(textbox)
  88.     o_cols = int(scribus.getColumns(textbox))
  89.     o_gap = scribus.getColumnGap(textbox)
  90.             
  91.     columns_width = 0
  92.     column_pos = 0
  93.     o_colwidth = (o_width - ((o_cols - 1) * o_gap)) / o_cols
  94.     if (o_cols > 1):
  95.         while (columns_width > o_cols or columns_width < 1):
  96.             columns_width = scribus.valueDialog('Width',
  97.                                             'How many columns width shall the '+
  98.                                             'box be (max ' + str(o_cols) + ')?','1')
  99.             columns_width = int(columns_width)
  100.         if (columns_width < o_cols):
  101.             max = o_cols - columns_width
  102.             while (column_pos <= max and column_pos <= 1):
  103.                 column_pos = scribus.valueDialog('Placement',
  104.                                          'In which column do you want '
  105.                                          'to place the box (1 to ' +
  106.                                          str(o_cols) + ')?','1')
  107.             column_pos = int(column_pos) - 1 
  108.     if (o_cols == 1):
  109.     columns_width = 1
  110.     new_height = 0
  111.     while (new_height == 0):
  112.         new_height = scribus.valueDialog('Height','Your frame height is '+ str(o_height) +
  113.                                                  unitlabel +'. How tall\n do you want your ' +
  114.                                                  'infobox to be in '+ unitlabel +'?\n If you load an image, height will be\n calculated, so the value here does not\n matter.', str(o_height))
  115.     new_top = -1
  116.     while (new_top < 0):
  117.         new_top = scribus.valueDialog('Y-Pos','The top of your infobox is currently\n'+ str(top) +
  118.                                                  unitlabel +'. Where do you want \n' +
  119.                                                  'the top to be in '+ unitlabel +'?', str(top))
  120.     framename = scribus.valueDialog('Name of Frame','Name your frame or use this default name',"infobox" + str(boxcount) + textbox)
  121.     frametype = 'text'
  122.     frametype = scribus.valueDialog('Frame Type','Change to anything other\n than "text" for image frame.\nEnter "imageL" to also load an image',frametype)
  123.     new_width = columns_width * o_colwidth + (columns_width-1) * o_gap
  124.     new_left = left + ((column_pos) * o_colwidth) + ((column_pos) * o_gap)
  125.     if (frametype == 'text'):
  126.         new_textbox = scribus.createText(new_left, float(new_top), new_width, float(new_height),framename)
  127.         scribus.setColumnGap(0, new_textbox)
  128.         scribus.setColumns(1, new_textbox)
  129.         scribus.textFlowMode(new_textbox, 1)
  130.     else:
  131.         if (frametype == 'imageL'):
  132.         imageload = scribus.fileDialog('Load image','Images(*.jpg *.png *.tif *.JPG *.PNG *.jpeg *.JPEG *.TIF)',haspreview=1)
  133.             im = Image.open(imageload)
  134.             xsize, ysize = im.size
  135.         new_height = float(ysize)/float(xsize)*new_width
  136.         new_image = scribus.createImage(new_left, float(new_top), new_width, float(new_height),framename)
  137.         scribus.loadImage(imageload, new_image)
  138.         else:
  139.         new_image = scribus.createImage(new_left, float(new_top), new_width, float(new_height),framename)
  140.         scribus.textFlowMode(new_image, 1)
  141.         scribus.setScaleImageToFrame(scaletoframe=1, proportional=1, name=new_image)
  142. if __name__ == '__main__':
  143.     # This script makes no sense without a document open
  144.     if not scribus.haveDoc():
  145.         scribus.messageBox('Scribus - Script Error', "No document open", scribus.ICON_WARNING, scribus.BUTTON_OK)
  146.         sys.exit(1)
  147.     # Disable redraws
  148.     scribus.setRedraw(False)
  149.     # Run the main script, ensuring redraws are re-enabled even if the
  150.     # script aborts with an exception, and don't fail with an exception
  151.     # even if the document is closed while the script runs.
  152.     try:
  153.         main(sys.argv)
  154.     finally:
  155.         try:
  156.             scribus.setRedraw(True)
  157.         except:
  158.             pass
  159.  
  160.